package com.example.yunhailutong.zhongtan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yunhailutong.zhongtan.Adapter.CommonRecyclerViewAdapter;
import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.InfoListEntity;
import com.example.yunhailutong.zhongtan.entity.InfoListEntity.DataBean;
import com.example.yunhailutong.zhongtan.net.PersionBiz;
import com.example.yunhailutong.zhongtan.util.DynamicTimeFormat;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;

import static android.widget.LinearLayout.VERTICAL;

public class MessageListActivity extends AppCompatActivity{
    private RefreshLayout mRefreshLayout;
    private ClassicsHeader mClassicsHeader;
    private static boolean isFirstEnter = true;
    private static String infoType = "25";
    private List<DataBean> mDatas = new ArrayList<>();
    private InfoListEntity infoListEntity;
    Context context = null;
    private static int pageIndex = 0;
    TextView textView;
    ImageView back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_list);
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        context = this.context;
        mRefreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout);
        textView = findViewById(R.id.tv_message_list);
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        final Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        infoType = bundle.getString("type");
        if (infoType.equals("25")) {
            textView.setText("党员教育");
        } else if (infoType.equals("26")) {
            textView.setText("党员风采");
        }
        Map<String, String> params = new HashMap<>();
        params.put("type", infoType);
        params.put("pageIndex", "1");
        params.put("pageSize", "10");
        new PersionBiz().getInfoList(params);

        int delta = new Random().nextInt(7 * 24 * 60 * 60 * 1000);
        mClassicsHeader = (ClassicsHeader)mRefreshLayout.getRefreshHeader();
        mClassicsHeader.setLastUpdateTime(new Date(System.currentTimeMillis()-delta));
        mClassicsHeader.setTimeFormat(new SimpleDateFormat("更新于 MM-dd HH:mm", Locale.CHINA));
        mClassicsHeader.setTimeFormat(new DynamicTimeFormat("更新于 %s"));
    }

    private CommonRecyclerViewAdapter.OnRecyclerviewItemClickListener onRecyclerviewItemClickListener = new CommonRecyclerViewAdapter.OnRecyclerviewItemClickListener() {
        @Override
        public void onItemClickListener(View v, int position) {
            //这里的view就是我们点击的view  position就是点击的position
            Intent intent = new Intent(v.getContext(), MessageDetailsActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("content", mDatas.get(position).getIm_content());
            bundle.putString("title", mDatas.get(position).getIm_title());
            bundle.putString("img_url", mDatas.get(position).getIm_imgPath());
            intent.putExtras(bundle);
            v.getContext().startActivity(intent);
        }
    };

    @Subscribe
    public void log(OldsEvent.GetInfoListEvent msg) {
        if (msg.what == -1) {
            Toast.makeText(this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
        } else {
            infoListEntity = (InfoListEntity)msg.obj;
            for(DataBean dataBean: infoListEntity.getData()) {
                mDatas.add(dataBean);
            }
            View view = findViewById(R.id.rv_message_list);
            if (view instanceof RecyclerView) {
                RecyclerView recyclerView = (RecyclerView) view;
                recyclerView.setLayoutManager(new LinearLayoutManager(this));
                recyclerView.addItemDecoration(new DividerItemDecoration(this, VERTICAL));
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                recyclerView.setAdapter(new CommonRecyclerViewAdapter(recyclerView.getContext(), mDatas, onRecyclerviewItemClickListener));
            }

            mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    pageIndex++;
                    Map<String, String> params = new HashMap<>();
                    params.put("type", infoType);
                    params.put("pageIndex", String.valueOf(pageIndex));
                    params.put("pageSize", "10");
                    new PersionBiz().getInfoList(params);

                    refreshLayout.finishRefresh();
                }
            });

            mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                    pageIndex++;
                    Map<String, String> params = new HashMap<>();
                    params.put("type", infoType);
                    params.put("pageIndex", String.valueOf(pageIndex));
                    params.put("pageSize", "10");
                    new PersionBiz().getInfoList(params);
                    refreshLayout.finishLoadMore();      //加载完成
                }
            });

//            if (isFirstEnter) {
//                isFirstEnter = false;
//                //触发自动刷新
//                mRefreshLayout.autoRefresh();
//            }
        }


    }

}
